var hAtomBlock = {};

hAtomBlock.Form = {

	"fnames": [
		"rowid", "title", "published_dt", "published_h", "published_m", "published_a",
		 "updated_dt", "updated_h", "updated_m", "updated_a", "author", "summary", "content", "pid"
	],

	setValues: function(entry) {
		for (var i = 0; i < this.fnames.length; i++) {
			var key = this.fnames[i];
			$("[name='" + key + "']").val(entry[key]);
		}
	},

	getValues: function() {
		var entry = {};
		for (var i = 0; i < this.fnames.length; i++) {
			var key = this.fnames[i];
			entry[key] = $("[name='" + key +"']").val();
		}
		var published = "";
		$("[id*='published']").each(function(key,el) {
			published += ($(el).attr("id") == "published_m") ? ":" + $(el).val() : " " + $(el).val();
		});

		var updated = "";
		$("[id*='updated']").each(function(key,el){
			updated += ($(el).attr("id") == "updated_m") ? ":" + $(el).val() : " " + $(el).val();
		});
		entry.published = $.trim(published);
		entry.updated = $.trim(updated);
		return entry;
	},

	clear: function() {
		for (var i = 0; i < this.fnames.length; i++) {
			var key = this.fnames[i];
			$("#" + key).val("");
		}
	}

}

hAtomBlock.Entries = {

	"fnames": ["title","summary","published", "updated"],
	"entries": "#feedList tr",
	"select": "#feedList tr#rid",

	add: function(entry) {
		entry.rowid	= $(this.entries).length;

		$("#feedList tr:last").after("<tr id=\"rid" + entry.rowid +  "\" />");
		$("#feedList tr:last").append("<td class='title'>" + entry.title + "</td>");
		$("#feedList tr:last").append("<td class='summary'>" + entry.summary + "</td>");
		$("#feedList tr:last").append("<td class='published'>" + entry.published + "</td>");
		$("#feedList tr:last").append("<td class='updated'>" + entry.updated + "</td>");
		$("#feedList tr:last").append("<td>");
		var editButtun = $("#feedList tr:last td:last").append("<a class=\"edit\" href=\"#\">edit</a>");
		$("#feedList tr:last").append("<td>");
		var deleteButtun	 = $("#feedList tr:last td:last").append("<a class=\"delete\" href=\"#\">delete</a>");

		editButtun.bind("click", {"rowid": entry.rowid}, function(event) {
			var entry = hAtomBlock.Parameters.get(event.data.rowid);
			hAtomBlock.Form.setValues(entry);
		});

		deleteButtun.bind("click", {"entry": entry, "select": this.select}, function(event) {
			var entry = event.data.entry;
			var select = event.data.select;
			$(select + entry.rowid.toString()).remove();
			hAtomBlock.Parameters.remove(entry.rowid);
		});
		return entry;
	},

	update: function(entry) {
		var row = "#feedList tr#rid" + entry. rowid;
		for (var i = 0; i < this.fnames.length; i++) {
			var key = this.fnames[i];
			$(row + " td."  + key).html(entry[key]);
		}
	}

};

hAtomBlock.Parameters = {

	"insert": "#hatomForm input:last",
	"select": "#hatomForm input#",
	"prefix": "p",
	"fnames": [
		"RowId", "Title", "Published", "Updated", "Author", "Summary", "Content", "Pid",
		"Published_Dt", "Published_H", "Published_M", "Published_A"	,
		"Updated_Dt", "Updated_H", "Updated_M", "Updated_A"
	],

	get: function(rowid) {
		var prefix = this.prefix + rowid;
		var entry = {};
		for (var i = 0; i < this.fnames.length; i++) {
			var key = this.fnames[i].toLowerCase(), name = this.fnames[i];
			entry[key] = $(this.select + prefix  + name).val();
		}
		return entry;
	},

	add: function(entry) {
		var prefix = this.prefix + entry.rowid;
		var p = $("#hatomForm");
		for (var i = 0; i < this.fnames.length; i++) {
			var key = this.fnames[i].toLowerCase(), name = this.fnames[i];
			p.append("<input type=\"hidden\" />");
			$(this.insert).attr({"id": prefix  + name, "name": key + "[]", "value": entry[key]});
		}
	},

	update: function(entry) {
		var prefix = this.prefix + entry.rowid;
		for (var i = 0; i < this.fnames.length; i++) {
			var key = this.fnames[i].toLowerCase(), name = this.fnames[i];
			$(this.select + prefix  + name).val(entry[key]);
		}
	},

	remove: function(rowid) {
		var prefix = this.prefix + rowid;
		for (var i = 0; i <  this.fnames.length; i++) {
			var  name = this.fnames[i];
			$(this.select + prefix  + name).remove();
		}
	}
	
}


$(document).ready(function() {
	
	$("#add").click(function(){
		var entry = hAtomBlock.Form.getValues();
		entry = hAtomBlock.Entries.add(entry);
		hAtomBlock.Parameters.add(entry);
		hAtomBlock.Form.clear();
	});

	$("#update").click(function(){
		var entry = hAtomBlock.Form.getValues();
		hAtomBlock.Entries.update(entry);
		hAtomBlock.Parameters.update(entry);
		hAtomBlock.Form.clear();
	});


	$("#feedList tr td a.edit").each(function(index,el){
		var id = index + 1;
		$(el).bind("click", {"rowid": id}, function(event) {
			var entry = hAtomBlock.Parameters.get(event.data.rowid);
			hAtomBlock.Form.setValues(entry);
		});
	});

	var select = "#feedList tr#rid";
	$("#feedList tr td a.delete").each(function(index,el) {
		var id = index + 1;
		var e = hAtomBlock.Parameters.get(id);
		$(el).bind("click", {"entry": e, "select": select}, function(event){
			var entry = event.data.entry;
			var select = event.data.select;
			$(select + entry.rowid.toString()).remove();
			hAtomBlock.Parameters.remove(entry.rowid);
		});
	});

});



$(function() {	





/*
var hCardValidater ={

	validate:function(){
		var familyName = $("#familyName");
		if (!familyName.val()) {
			alert(ccm_t("familyName"));
			return false;
		}
 
		var givenName = $("#givenName");
		if (!givenName.val()) {
			alert(ccm_t("givenName"));
			return false;
		}

		var url = $("#url");
		if (!url.val()) {
			alert(ccm_t("url"));
			return false;
		} else if (!/^(((ht|f)tp(s?))\:\/\/)([0-9a-zA-Z\-]+\.)+[a-zA-Z]{2,6}(\:[0-9]+)?(\/\S*)?$/.test(url.val())) {
			alert(ccm_t("urlValid"));
			return false;
		}
		
		var email = $("#email");
		if (!email.val()) {
			alert(ccm_t("email"));
			return false;
		} else if (!/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/.test(email.val())) {
			alert(ccm_t("emailValid"));
			return false;
		}

		return true;
	}
}

ccmValidateBlockForm = function() { return hCardValidater.validate(); }
*/

});